﻿using CommonUtils;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsForms国密GmUtil
{
    public partial class Form1 : CommonUtils.FormBase
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        //签名
        private void btnSign_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtPrivateKey.Text) || string.IsNullOrWhiteSpace(txtUserId.Text) || string.IsNullOrWhiteSpace(txtToSignStr.Text))
                {
                    MessageBox.Show("私钥,USERID,签名字符串不能为空。");
                    return;
                }

                // 由d生成私钥 ---------------------
                BigInteger d = new BigInteger(txtPrivateKey.Text, 16);
                ECPrivateKeyParameters bcecPrivateKey = CommonUtils.GmUtil.GetPrivatekeyFromD(d);
                byte[] userId = Encoding.UTF8.GetBytes(txtUserId.Text);

                byte[] msg = System.Text.Encoding.UTF8.GetBytes(txtToSignStr.Text);
                byte[] sig = GmUtil.SignSm3WithSm2(msg, userId, bcecPrivateKey);
                
                //byte[] bytTmp = Hex.Decode(txtSign.Text);
                txtSign.Text = Convert.ToBase64String(sig);

                string sign16 = Hex.ToHexString(sig);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        //验签
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtPubKey.Text) || string.IsNullOrWhiteSpace(txtUserId.Text) || string.IsNullOrWhiteSpace(txtToSignStr.Text)
                    || string.IsNullOrWhiteSpace(txtSign.Text))
                {
                    MessageBox.Show("公钥,USERID,签名字符串，签名值不能为空。");
                    return;
                }
                //公钥128位，拆成2个64位。
                string pa = txtPubKey.Text.Substring(0, 64);
                string pb = txtPubKey.Text.Substring(64, 64);

                AsymmetricKeyParameter publicKey1 = GmUtil.GetPublickeyFromXY(new BigInteger(pa, 16), new BigInteger(pb, 16));

                byte[] userId = Encoding.UTF8.GetBytes(txtUserId.Text);
                byte[] msg = System.Text.Encoding.UTF8.GetBytes(txtToSignStr.Text);
                //byte[] bytSig = Hex.Decode(txtSign.Text);
                byte[] bytSig = Convert.FromBase64String(txtSign.Text);
                bool bRst = GmUtil.VerifySm3WithSm2(msg, userId, bytSig, publicKey1);
                if (bRst)
                {
                    lblValidRst.Text = "验证：通过 "+DateTime.Now.ToString();
                }
                else
                {
                    lblValidRst.Text = "验证：不通过 " + DateTime.Now.ToString();
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }


    }
}
